<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>Mongo-Web学生管理系统</title>
    <link rel="stylesheet" href="https://unpkg.com/element-ui@2.9.0/lib/theme-chalk/index.css">
</head>

<body>
<div id="app">
    <el-container>
        <el-header>
            <h1>欢迎使用学生信息管理系统</h1>

        </el-header>
        <el-main>
            <el-form ref="form" :model="form" :inline="true" label-width="80px">
                <el-form-item label="学号">
                    <el-input v-model="form.sno"></el-input>
                </el-form-item>
                <el-form-item label="姓名">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="身份证号">
                    <el-input v-model="form.idCard"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-select v-model="form.sex" placeholder="请选择性别">
                        <el-option label="全部" value=""></el-option>
                        <el-option v-for="s in sex" :key="s.code" :value="s.code"
                                   :label="s.name"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="专业">
                    <el-select v-model="form.major" placeholder="请选择专业">
                        <el-option label="全部" value=""></el-option>
                        <el-option v-for="m in majorList" :key="m.code" :value="m.code"
                                   :label="m.name"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="queryStudent">查找</el-button>
                    <el-button type="danger" @click="reset">重置</el-button>
                </el-form-item>
            </el-form>
            </el-row>
            <el-row type="flex" justify="start">
                <el-col>
                    <el-button-group>
                        <el-button type="success " icon="el-icon-plus" round @click="add">添加</el-button>
                        <el-button type="danger" icon="el-icon-delete" round @click="deleteVisible = true">删除
                        </el-button>
                        <el-button type="warning" icon="el-icon-download" round>导入</el-button>
                        <el-button type="success" icon="el-icon-upload2" round>导出</el-button>
                        <el-button type="info" icon="el-icon-printer" round>打印</el-button>
                    </el-button-group>
                </el-col>
            </el-row>
            <el-row class="table">
                <el-col>
                    <el-table
                            ref="multipleTable"
                            v-loading="loading"
                            element-loading-text="正在加在数据......."
                            element-loading-spinner="el-icon-loading"
                            :data="tableData"
                            tooltip-effect="dark"
                            style="width: 100%"
                            border
                            height="650"
                            @selection-change="handleSelectionChange">
                        <el-table-column
                                type="selection">
                        </el-table-column>
                        <el-table-column
                                prop="sno"
                                fixed="left"
                                width="160"
                                label="学号">
                        </el-table-column>
                        <el-table-column
                                prop="name"
                                fixed="left"
                                width="160"
                                label="姓名">
                        </el-table-column>
                        <el-table-column
                                prop="sex"
                                width="100"
                                label="性别">
                        </el-table-column>
                        <el-table-column
                                prop="birthDay"
                                width="160"
                                label="出生日期"
                                :formatter="dateFormat">
                        </el-table-column>
                        <el-table-column
                                prop="idCard"
                                width="240"
                                label="身份证号">
                        </el-table-column>
                        <el-table-column
                                prop="phone"
                                width="160"
                                label="电话号码">
                        </el-table-column>
                        <el-table-column
                                prop="major"
                                width="200"
                                label="专业">
                        </el-table-column>
                        <el-table-column
                                prop="address"
                                label="家庭住址"
                                show-overflow-tooltip>
                        </el-table-column>

                        <el-table-column label="操作" width="160">
                            <template slot-scope="scope">
                                <el-button
                                        size="mini"
                                        type="primary"
                                        @click="handleEdit(scope.$index, scope.row)">编辑
                                </el-button>
                                <el-button
                                        size="mini"
                                        type="danger"
                                        @click="handleDelete(scope.$index, scope.row)">删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-col>
            </el-row>

            <el-dialog title="添加学生" :visible.sync="addFormVisible">
                <el-form :model="addForm">
                    <el-row>
                        <el-col :span="12">
                            <input type="hidden" v-model="addForm._id">
                            <el-form-item label="学号" label-width="100px" placeholder="请输入学号">
                                <el-input v-model="addForm.sno" autocomplete="off" :disabled="true"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="身份证号" label-width="100px" placeholder="请输入身份证号">
                                <el-input v-model="addForm.idCard" autocomplete="off"
                                          @input="inputIdCard"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="姓名" label-width="100px" placeholder="请输入姓名">
                                <el-input v-model="addForm.name" autocomplete="off"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="性别" label-width="100px">
                                <el-select v-model="addForm.sex" placeholder="请选择性别" :disabled="editDisable"
                                           style="width:100%">
                                    <el-option v-for="s in sex" :key="s.code" :value="s.code"
                                               :label="s.name"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="系部" label-width="100px">
                                <el-select v-model="addForm.major" placeholder="请选择系部" style="width:100%">
                                    <el-option v-for="m in majorList" :key="m.code" :value="m.code"
                                               :label="m.name"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="输入日期" label-width="100px"
                                          placeholder="请选择出生日期">
                                <el-date-picker
                                        v-model="addForm.birthDay"
                                        type="date"
                                        placeholder="出生日期"
                                        style="width:100%;"
                                        value-format="yyyy-MM-dd"
                                        :disabled="editDisable">
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="年龄" label-width="100px"
                                          placeholder="请输入年龄">
                                <el-input-number
                                        :controls="true"
                                        v-model="addForm.age"
                                        :min="0"
                                        :max="100"
                                        :precision="0"
                                        style="width:100%;"
                                        :disabled="editDisable"
                                ></el-input-number>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">

                            <el-form-item label="电话号码" label-width="100px" placeholder="请输入电话号码">
                                <el-input v-model="addForm.phone" autocomplete="off"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="用户名" label-width="100px" placeholder="请输入用户名">
                                <el-input v-model="addForm.username" autocomplete="off"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="密码" label-width="100px" placeholder="请输入密码">
                                <el-input v-model="addForm.password" type="password" show-password
                                          autocomplete="off"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="家庭住址" label-width="100px" placeholder="请输入家庭住址">
                                <el-input v-model="addForm.address" type="textarea" autocomplete="off"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="addFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="save">确 定</el-button>
                </div>
            </el-dialog>


            <el-dialog
                    title="提示"
                    :visible.sync="deleteVisible"
                    width="30%">
                <span>你确定要删除这些学生吗?</span>
                <span slot="footer" class="dialog-footer">
    <el-button @click="deleteVisible = false">取 消</el-button>
    <el-button type="primary" @click="del">确 定</el-button>
  </span>
            </el-dialog>
        </el-main>
        <el-footer>
            宁夏财经职业技术学院
        </el-footer>
    </el-container>
</div>

</body>

<script src="https://unpkg.com/vue@2.7.13/dist/vue.js"></script>
<script src="https://unpkg.com/element-ui@2.9.0/lib/index.js"></script>
<script src="https://unpkg.com/axios@1.1.3/dist/axios.js"></script>
<script src="https://cdn.bootcss.com/qs/6.5.1/qs.js"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                sex: [{'code': '男', "name": "男"}, {'code': '女', "name": "女"}],
                majorList: [],
                form: {
                    sno: '',
                    name: '',
                    idCard: '',
                    sex: '',
                    major: ''

                },
                addForm: {
                    _id: 0,
                    sno: '',
                    name: '',
                    idCard: '',
                    age: 0,
                    sex: '',
                    birthDay: new Date(),
                    phone: '',
                    username: '',
                    password: '',
                    address: '',
                    major: '',
                    email: ''
                },
                tableData: [],
                loading: true,
                addFormVisible: false,
                deleteVisible: false,
                selectedRows: [],
                editDisable: false
            }
        },

        created() {
            this.queryStudent();
            this.queryMajor();
        },


        methods: {

            queryMajor() {
                const url = "/major/list.do?";
                const options = {
                    url,
                    method: 'POST'
                };
                axios(options)
                    .then(function (response) {
                        this.majorList = response.data;
                    }.bind(this));
            },
            queryStudent() {
                const url = "/student/list.do?";
                const options = {
                    url,
                    method: 'POST',
                    params: this.form
                };
                axios(options)
                    .then(function (response) {
                        this.tableData = response.data;
                        this.loading = false;
                    }.bind(this));
            },
            add() {
                const url = "/student/getMaxSno.do";
                const options = {
                    url,
                    method: 'POST'
                };
                axios(options)
                    .then(function (response) {
                        this.addForm.sno = parseInt(response.data.max) + 1;
                        this.addFormVisible = true;
                    }.bind(this));


            },
            save() {
                const url = "/student/save.do?";
                const options = {
                    url,
                    method: 'POST',
                    /*headers: {
                        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                    },*/
                    params: this.addForm
                };
                axios(options)
                    .then(function (response) {
                        if (response.data.code == 0) {
                            this.$message({
                                type: 'success',
                                message: '保存成功'
                            });
                            this.addFormVisible = false;
                            this.queryStudent();
                        }

                    }.bind(this));
            },
            del() {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteVisible = false;
                    if (this.selectedRows.length > 0) {
                        let snos = [];
                        for (var i = 0; i < this.selectedRows.length; i++) {
                            snos.push(this.selectedRows[i].sno)
                        }
                        const url = "/student/delete.do?";
                        const params = new URLSearchParams();
                        params.append("snos", snos.join(","));
                        const options = {
                            url,
                            method: 'POST',
                            params,
                            headers: {
                                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                            }
                        };
                        axios(options)
                            .then(function (response) {
                                if (response.data.code == 0) {
                                    this.$message({
                                        message: '删除成功',
                                        type: 'success'
                                    });
                                    this.queryStudent();
                                } else {
                                    this.$message({
                                        message: '删除失败',
                                        type: 'error'
                                    });
                                }
                            }.bind(this));

                    } else {
                        this.$message('没有选中的学生,请选择要删除的学生!');
                    }
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            inputIdCard() {
                let patten = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
                if (patten.test(this.addForm.idCard)) {
                    this.addForm.sex = parseInt(this.addForm.idCard.substring(16, 17)) % 2 == 0 ? "女" : "男";
                    this.addForm.age = new Date().getFullYear() - parseInt(this.addForm.idCard.substring(6, 10));
                    this.addForm.birthDay = new Date(
                        parseInt(this.addForm.idCard.substring(6, 10)),
                        parseInt(this.addForm.idCard.substring(10, 12)),
                        parseInt(this.addForm.idCard.substring(12, 14))
                    );
                    this.editDisable = true;
                } else {
                    this.editDisable = false;
                }
            },

            reset() {
                this.form.sno = "";
                this.form.name = "";
                this.form.idCard = "";
                this.form.sex = "";
                this.form.major = "";
            },
            handleSelectionChange(e) {
                this.selectedRows = e;
            },
            handleDelete(index, row) {
                this.$refs.multipleTable.toggleRowSelection(row);
                this.del();
            },
            handleEdit(index, row) {
                this.addFormVisible = true;
                this.addForm = row;
            },
            dateFormat: function (row, column) {
                var _date = row[column.property];
                if (_date === undefined) {
                    return ''
                }
                var date = new Date(_date);
                var year = date.getFullYear();
                var month = date.getMonth() + 1;
                var dates = date.getDate();
                if (month < 10) month = "0" + month;
                if (dates < 10) dates = "0" + dates;
                return year + "-" + month + "-" + dates
            }


        }
    })
</script>

<style>
    .el-footer {
        width: 100%;
        overflow: hidden;
        text-align: center;
        line-height: 60px;
        background-color: #1e9fff;
    }

    .el-header {
        text-align: center;
        background-color: #1e9fff;
    }

    .el-main {
        text-align: center;
        min-height: 100%;
    }

    body > .el-container {
        margin-bottom: 40px;
    }

    .table {
        margin-top: 10px;
    }


</style>
</html>